
#include "sdk/sdk.h"
#include "sdk/misc.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>

using Pos = SDK::Pos;
using PPlayerInfo = SDK::PPlayerInfo;
using PCommand = SDK::PCommand;
using PShowInfo = SDK::PShowInfo;
using Edge = PPlayerInfo::Edge;
using Robot = PPlayerInfo::Robot;
using HumanOrder = PPlayerInfo::HumanOrder;
using Buff = Robot::Buff;
using Skill = Robot::Skill;
using Operation = PCommand::Operation;
using ShowInfo = PShowInfo::ShowInfo;

PPlayerInfo playerInfo;
PCommand pCommand;
PShowInfo *pShowInfo;

//人工操作解析器
class Parser
{
	public:
		//这个解析器会将所有人工操作显示在播放器的控制台中
		void parse(const HumanOrder& order)
		{
			switch (order.type)
			{
			}
		}

		void parse(const vector<HumanOrder>& orders)
		{
			for (unsigned int i = 0; i < orders.size(); i++)
				parse(orders[i]);
		}
}parser;


//样例AI
class SampleAI1
{
private:
	//对局开始的初始化
	void init()
	{
		//srand(time(0));
	}

public:

	//主过程，通过playerInfo得到pCommand
	Pos solve()
	{
		//思路：按照随机顺序对每个我方机器人发布可行的随机指令
        pCommand.cmds.clear();
		if (playerInfo.round == 0)
			init();

		vector<Robot*>MyRobot, EnemyRobot;//我方机器人列表，敌方机器人列表
		vector<Pos>BuildAblePos;//当前地图上的空地
		//构造MyRobot, EnemyRobot
		for (unsigned int i = 0; i < playerInfo.robots.size(); i++)
		{
			Robot& obj = playerInfo.robots[i];
			if (obj.team == playerInfo.team)
				MyRobot.push_back(&obj);
			else
				EnemyRobot.push_back(&obj);
		}

		for (int x = 0; x < MAP_SIZE; x++)
			for (int y = 0; y < MAP_SIZE; y++)
				if (MAP[x][y] == 1 && playerInfo.RobotAt(Pos(x, y)) == NULL)
					BuildAblePos.push_back(Pos(x, y));
		random_shuffle(MyRobot.begin(), MyRobot.end());
		random_shuffle(EnemyRobot.begin(), EnemyRobot.end());
		random_shuffle(BuildAblePos.begin(), BuildAblePos.end());

		for (unsigned int id = 0; id < MyRobot.size(); id++)
		{
			Robot* obj = MyRobot[id];

			for (int i = 0; i < EnemyRobot.size(); i++)
			{
				pCommand.AddOrder(OrderType::PlainAttack, obj->pos, EnemyRobot[i]->pos, rand()%100);
			}

			for (int i = 0; i < BuildAblePos.size(); i++)
			{
				pCommand.AddOrder(OrderType::Split, obj->pos, BuildAblePos[i], rand()%100);
			}

			pCommand.AddOrder(OrderType::Evolve, obj->pos, RobotType::GatherRobot);
		}
		return MyRobot[rand() % MyRobot.size()]->pos;
	}

}sampleAi1;


//普通AI接口，一回合调用一次
extern "C"
{
void player_ai(const PlayerInfo &playerInfo_, Commands &pCommand_, PShowInfo &pShowInfo_) {
    //这里将传入的PlayerInfo转化为PPlayerInfo格式
	playerInfo = SDK::interpretPlayerInfo(playerInfo_);
	pShowInfo = &pShowInfo_;
    parser.parse(playerInfo.orders);
    Pos target = sampleAi1.solve();
    //cout << "target: " << target.x << ',' << target.y << '\n';	pCommand.AddAutoTransfer(playerInfo, target);//能量调度
    pCommand_ = SDK::translateCommands(playerInfo, pCommand);
    pCommand.AddAutoTransfer(playerInfo, target);//能量调度
    return;
}
}
//高速AI接口，播放器一帧调用一次
void feedback_ai(vector<HumanOrder> orders, PShowInfo& pShowInfo)
{
	//在这里进行一些处理
	return;
}
